package com.kr.weitao.util.rest.query;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.kr.weitao.exception.CommonException;
import com.kr.weitao.util.StringUtils;
import com.kr.weitao.util.rest.schema.Function;
import com.kr.weitao.util.spring.SpringUtils;
import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

/** Created by paco on 2017/06/09. */
@Log4j2
@Component
public class ObjectDelete {

  public JSONObject delete(JSONObject param) {

    JSONObject resultJo = new JSONObject();
    long successcount = 0;
    long errorcount = 0;
    long allcount = 0;
    int dcount = 0; //每次删除数据量

    long user_id = 0; //用户ID
    Function table = null; //数据表
    String condigion = null; //数据删除方式，默认为通过ID删除，另外支持通过条件删除
    JSONObject master_tableinfo = null; //主表数据
    String message = "";

    //region 参数判断
    //region 1、系统参数判断
    if (param == null || param.isEmpty()) {
      log.error("param is null");
      throw new CommonException("-1", "参数不能为空");
    }
    //endregion

    //region 2、获取用户信息
    if (!param.containsKey("user_id")) {
      log.error("param not has user_id,param;" + param.toJSONString());
      throw new CommonException("-3", "缺少用户信息");
    }

    user_id = param.getLongValue("user_id");
    if (user_id == 0) {
      log.error("param user_id error,param;" + param.toJSONString());
      throw new CommonException("-3", "缺少用户信息");
    }
    //endregion

    //region 3、业务参数判断
    if (!param.containsKey("function")) {
      log.error("data not has table key:" + param);
      throw new CommonException("-3", "请选择要操作的菜单");
    }

    String table_name = param.getString("function");
    if (StringUtils.isNull(table_name)) {
      log.error("data table is null:" + param);
      throw new CommonException("-3", "请选择要操作的菜单");
    }

    master_tableinfo = param.getJSONObject("master_object");
    if (master_tableinfo == null || master_tableinfo.isEmpty()) {
      log.error("master_object datainfo is null:" + param);
      throw new CommonException("-3", "请维护要删除的数据");
    }

    if (master_tableinfo.containsKey("id")) {
      condigion = "by_id";
    } else if (master_tableinfo.containsKey("condition")) {
      condigion = "by_condition";
    }
    if (StringUtils.isNull(condigion)) {
      log.error(
          "delete data error not has key or has condition param:"
              + master_tableinfo.toJSONString());
      throw new CommonException("-80", "请维护要删除的数据");
    }

    //endregion
    //endregion

    //region 数据删除
    if ("by_id".equalsIgnoreCase(condigion)) {
      JSONArray deleteja = master_tableinfo.getJSONArray("id");
      if (deleteja == null || deleteja.size() <= 0) {
        throw new CommonException("-1", "请选择要删除的数据");
      }

      allcount = deleteja.size();
      long deleteDataId = 0;

      for (int i = 0; i < allcount; i++) {
        deleteDataId = deleteja.getLongValue(i);

        try {
          JSONObject deleteParam = new JSONObject();
          deleteParam.put("function", table_name);
          deleteParam.put("id", deleteDataId);
          if(param.containsKey("def_wx_public_id")){
            deleteParam.put("def_wx_public_id", param.getString("def_wx_public_id"));
          }
          dcount = SpringUtils.getBean(ObjectDeleteUtil.class).execute(deleteParam);
          if (dcount > 0) {
            successcount += 1;
          } else {
            errorcount += 1;
          }
        } catch (CommonException e) {
          log.error("delete data error:" + e.getMessage(), e);
          errorcount += 1;
          message = e.getMessage();
        }
      }
    } else {

    }
    //endregion

    //region 组织返回数据
    if (errorcount > 1) {
      message = "删除失败";
    } else if (allcount == successcount) {
      message = "删除成功";
    }

    resultJo.put("allcount", allcount);
    resultJo.put("successCount", successcount);
    resultJo.put("errcount", errorcount);
    resultJo.put("msg", message);
    //endregion

    return resultJo;
  }
}
